package com.android.ailaoshan.voice;

import android.os.Environment;

import com.android.ailaoshan.tools.ManagerRecorder;
import com.android.ailaoshan.voice.model.VoiceInfo;

import java.io.File;

/**
 * 音频文件管理
 * 音频文件增删查
 */
public class ManagerVoice {

    /** 录音文件存储路径 */
    private final String VOICE_PATH = Environment.getExternalStorageDirectory().getAbsolutePath()
            + File.separator +"AiLaoShan"+ File.separator + "Voice" +File.separator;

    /**
     * 录音完成后处理
     */
    public void doneVoice(int RESULT_CODE,String voiceName){
        String filePath = VOICE_PATH + voiceName;
        File file = new File(filePath);

        // 录音成功
        if(RESULT_CODE == -1){
            addVoice(voiceName);
        }else{
            file.delete();
        }
    }

    /**
     * 数据库增加音频文件
     */
    public void addVoice(String voiceName){
        new Thread(
            ()->{
                // 查询未完成的记录ID
                ManagerRecorder managerRecorder = new ManagerRecorder();

                VoiceInfo voiceInfo = new VoiceInfo();
                voiceInfo.setrName(voiceName);
                voiceInfo.setrTime(System.currentTimeMillis());
                voiceInfo.setrId(managerRecorder.getUnfinishedId());

                // 查询经纬度信息

                voiceInfo.save();
            }
        ).start();
    }
}
